package com.atguigu.tingshu.album.config;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * title:
 * author: zgl
 * date: 2024/3/13
 * description：
 */
@Configuration
public class BloomFilterConfig {
    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private AlbumInfoMapper albumInfoMapper;

    @PostConstruct
    public void init() {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        bloomFilter.tryInit(1000000,0.03);
        List<AlbumInfo> albumInfoList = this.albumInfoMapper.selectList(null);
        albumInfoList.forEach(albumInfo -> {
            bloomFilter.add(RedisConstant.ALBUM_INFO_PREFIX + albumInfo.getId());
        });
    }
}
